package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.VReportImport;
import net.sf.json.JSONObject;

import org.apache.commons.fileupload.FileUploadException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import annotation.Log;
import persistent.factory.DAOFactory;
import util.office.GetEPQorMHTForWord;
import util.toolkit.Constant;
import util.toolkit.ResponseJSON;

/**
 * 数据导入处理类
 * @author 徐震霖
 * @since 2018-12-09 13:44
 */
@Controller
@RequestMapping(value="/dataimportmanagement")
public class DataImportManagement {
	
	private static List<VReportImport> reportlist = new ArrayList<VReportImport>();
	
	/**
	 * 批量导入EPQ和MHT报告控制类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 批量导入EPQ和MHT报告控制类
	@Log(operationType = Constant.OPERTATION_ADD, operationDescribe = "批量导入EPQ和MHT报告",
			operationClassPath = "controller.DataImportManagement.importEPQandMHTList(Model model, HttpServletRequest req,HttpServletResponse resp)",
			operationMenu = "数据导入管理/数据导入管理")
	@RequestMapping(value="/importepqandmhtlist")
	public String importEPQandMHTList(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		GetEPQorMHTForWord word = new GetEPQorMHTForWord();
		HttpSession session = req.getSession();
		
		try {
			String classid = req.getParameter("classid");
			String schoolid = req.getParameter("schoolid");
			
			if (classid != null && !classid.equals("") && schoolid != null && !schoolid.equals("")) {
				VReportImport reportImport = word.readWordFile(req, resp, classid, schoolid);
				reportlist.add(reportImport);
				session.setAttribute("reportlist", reportlist);
				result.put("code", ResponseJSON.FLAG_SUCC);
				result.put("msg", "数据获取成功！");
			}
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "班级编号不能为空，数据获取失败！");
			}
		} 
		catch (IOException e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "数据获取失败！");
			e.printStackTrace();
		}
		catch (FileUploadException e) {	
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "数据获取失败！");
			e.printStackTrace();
		}
		catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "数据获取失败！");
			e.printStackTrace();
		}		
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
		
	}
	
	/**
	 * 保存测评结果处理类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 保存测评结果处理类
	@Log(operationType = Constant.OPERTATION_ADD, operationDescribe = "保存测评结果",
			operationClassPath = "controller.DataImportManagement.saveEPQandMHTList(Model model, HttpServletRequest req,HttpServletResponse resp)",
			operationMenu = "无页面调用")
	@RequestMapping(value="/saveepqandmhtlist")
	public String saveEPQandMHTList(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		HttpSession session = req.getSession();
		
		try {
			List<VReportImport> reportlist1 = (List<VReportImport>) session.getAttribute("reportlist");
			if (reportlist1 == null || reportlist1.size() <= 0) {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "请先导入数据后再保存！");
			}
			else {
				if(DAOFactory.getDataImportDAO().importEPQandMHTList(reportlist1)){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "数据保存成功！");
					session.removeAttribute("reportlist");
					reportlist.clear();
				}
				else {
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "数据重复或发生其他错误，数据保存失败，请检查数据后重试！");
				}
			}			
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_SUCC);
			result.put("msg", "数据重复或发生其他错误，数据保存失败，请检查数据后重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
		
	}
	
	/**
	 * 清空导入时session和reportlist里的数据
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 清空导入时session和reportlist里的数据
	@RequestMapping(value="/deleteepqormhtdate")
	public String deleteEPQorMHTDate(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		HttpSession session = req.getSession();
		
		try {
			session.removeAttribute("reportlist");
			if (reportlist.size() > 0 && reportlist != null) {
				reportlist.clear();
			}
			result.put("code", ResponseJSON.FLAG_SUCC);
			result.put("msg", "数据清空成功！");
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_SUCC);
			result.put("msg", "出现异常，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
		
	}

}
